import productService from '../../services/product'
import * as types from '../mutation-types'
// initial state
const state = {
  productCategory: [],
  subject:[]
}

// getters
const getters = {

}

// actions
const actions = {
  loadProductCategory({commit},params) {
    return new Promise((resolve,reject) =>{
      productService.loadCategoryServer(params, (data) =>{
        commit(types.LOAD_PRODUCTCATEGORY,data);
        resolve(data);
      });
    })
  },
  loadSubjectData({commit},params) {
    return new Promise((resolve,reject) => {
      productService.loadSubjectServer(params,(data) => {
        commit(types.LOAD_SUBJECT,data);
        resolve(data);
      });
    })
  }
}

// mutations
const mutations = {
  [types.LOAD_PRODUCTCATEGORY] (state, data) {
    state.productCategory = [{id: '',name:'全部',children: data.data}];
    if (data.data.children){
      for(var i=0;i<data.data.children.length;i++) {
        let item = data.data.children[i];
        if(item.children && item.children.length>0) {
          state.productCategory.push(item);
        }
      }
    }
    // state.productCategory = data.data;
  },
  [types.LOAD_SUBJECT] (state, data) {
    data.data.forEach(item =>{
      state.subject.push({
        name:item.name,
        value:item.id,
      })
    })
    // state.subject = data.data;
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
